home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 8: LINUX Games
/
Linux Cubed Series 8 - LINUX Games.iso
/
games
/
x11
/
tkbrain-.0
/
tkbrain-
/
tkbrain
/
tkBrain
< prev
next >
Wrap
Text File
|
1996-01-14
|
9KB
|
329 lines
#!/usr/local/bin/wish
# tkBrain.tcl -- a game in tcl/tk
# Author : JΘr⌠me Santini <js@zoe4>
# Created On : Fri Jan 12 18:11:52 1996
# Last Modified By: JΘr⌠me Santini <js@zoe4>
# Last Modified On: Sun Jan 14 18:05:15 1996
# File Path : /home/js/tmp/tkbrain/tkBrain
# Update Count : 442
# initial height
set taille_x 4
# initial width
set taille_y 5
# --------------------------------------------------------------------
proc randomize { liste } {
set cmd [open [concat |randomize $liste] r]
set liste [gets $cmd]
close $cmd
return $liste
}
# --------------------------------------------------------------------
proc add_one { varname i j v} {
global taille_y
set num [expr $i * $taille_y + $j]
global $varname$num
eval [format "set k $%s" $varname$num]
if { [string compare $k " "] != 0 } {
if { $v == 1} {
set $varname$num [expr $k == 4 ? 1 : $k + 1]
} else {
set $varname$num [expr $k == 1 ? 4 : $k - 1]
}
}
}
# --------------------------------------------------------------------
proc do_click { varname num v } {
global $varname$num taille_y taille_x
set $varname$num $v
set i [ expr $num / $taille_y ]
set j [ expr $num % $taille_y ]
# puts "--> $num -> $i, $j"
if { [string compare $v " "] != 0 } {
.d.$varname.l$i.c$j configure -state disabled -relief sunken
} else {
.d.$varname.l$i.c$j configure -state normal -relief raised
set v -1
}
if { $i > 0 } {
add_one $varname [expr $i - 1 ] $j $v
}
if { $i < [expr $taille_x - 1] } {
add_one $varname [expr $i + 1 ] $j $v
}
if { $j > 0 } {
add_one $varname $i [expr $j - 1 ] $v
}
if { $j < [expr $taille_y - 1 ] } {
add_one $varname $i [expr $j + 1 ] $v
}
}
# --------------------------------------------------------------------
# button's command
proc click { num } {
global undo_list
set undo_list [concat $num $undo_list]
do_click aire $num 1
redo_infos
}
# --------------------------------------------------------------------
proc undo { } {
global taille_x taille_y undo_cnt undo_list
if { [llength $undo_list] < 1 } {
.infos.text configure -text "Nothing to undo"
return;
}
incr undo_cnt
set num [lindex $undo_list 0]
set undo_list [lreplace $undo_list 0 0]
do_click aire $num " "
redo_infos
}
# --------------------------------------------------------------------
proc retry { } {
global taille_x taille_y retry_cnt undo_list
incr retry_cnt
for {set i 0} { $i < $taille_x } {incr i} {
for {set j 0} { $j < $taille_y } {incr j} {
set num [expr $i * $taille_y + $j ]
global aire$num
set aire$num " "
.d.aire.l$i.c$j configure -state normal -relief raised
}
}
set undo_list {}
redo_infos
}
# --------------------------------------------------------------------
proc redo_infos { } {
global retry_cnt undo_cnt taille_x taille_y undo_list
set text ""
set cnt [expr $taille_x * $taille_y]
if { $retry_cnt > 0} {
set text "$retry_cnt retry"
}
if { $undo_cnt > 0 } {
if { $retry_cnt > 0} {
set text [format "%s, " $text ]
}
set text [format "%s%d undo" $text $undo_cnt ]
}
if { [llength $undo_list] == $cnt} {
set perdu 0
for {set i 0} { $i < $cnt } { incr i } {
global aire$i modele$i
eval [format "set k $%s" aire$i]
eval [format "set l $%s" modele$i]
if {$k != $l} {
set perdu 1
}
}
set text [format "%s %s" $text \
[expr {($perdu == 1) ? "You lost" : "You won" } ] ]
}
# if { [string length $text]} {
.infos.text configure -text $text
# }
}
# --------------------------------------------------------------------
proc new_game { } {
initialise
}
# --------------------------------------------------------------------
proc show_text { justify text } {
global widg_cnt
incr widg_cnt
set tl .about$widg_cnt
toplevel $tl -class Dialog
wm title $tl [wm title .]
frame $tl.top -relief raised -bd 1
pack $tl.top -side top -fill both -expand true
frame $tl.bot -relief raised -bd 1
pack $tl.bot -side top -fill both -anchor s
message $tl.top.msg -text $text -aspect 500 -justify $justify
button $tl.bot.b -text Ok -command "destroy $tl"
pack $tl.top.msg -fill both -expand true
pack $tl.bot.b
}
# --------------------------------------------------------------------
proc about { } {
show_text center \
"tkBrain v1.0 by JΘr⌠me Santini\n(santini@soleil.univ-orleans.fr)\n\n(14.01.1996)"
}
proc help { } {
show_text left \
"The goal of this game is to reproduce the left grid on the right one. \
When you click on a square, it is set to one, and one is added to all the \
neighbours with a non-null value. If a square has a value of 4, \
then it is reset to 1"
}
# --------------------------------------------------------------------
proc resize { } {
global widg_cnt taille_x taille_y
incr widg_cnt
set tl .about$widg_cnt
toplevel $tl -class Dialog
wm title $tl [wm title .]
frame $tl.top -relief raised -bd 1
scale $tl.width -label Width -from 2 -to 10 -length 8c \
-orient horizontal
$tl.width set $taille_y
scale $tl.height -label Height -from 2 -to 10 -length 8c \
-orient horizontal
$tl.height set $taille_x
pack $tl.width $tl.height -in $tl.top -expand 1 -fill x
frame $tl.bot -relief raised -bd 1
button $tl.bot.ok -text Ok -command "do_resize $tl"
button $tl.bot.cancel -text Cancel -command "destroy $tl"
pack $tl.bot.ok $tl.bot.cancel -side left -expand 1
pack $tl.top -side top -fill both -expand 1
pack $tl.bot -side top -fill both -anchor s
}
# --------------------------------------------------------------------
proc do_resize { widg } {
global taille_x taille_y
set taille_x [$widg.height get];
set taille_y [$widg.width get];
destroy $widg ;
initialise
}
# --------------------------------------------------------------------
# initialisation de l'aire de jeux
proc initialise {} {
global undo_list taille_x taille_y retry_cnt undo_cnt
set undo_list {}
set liste {}
set retry_cnt 0
set undo_cnt 0
.infos.text configure -text "tkBrain by JΘr⌠me Santini"
destroy .d
frame .d
frame .d.modele -relief raised -bord 0
frame .d.sep -width 0.8c
frame .d.aire -relief raised -bord 0
for {set i 0} { $i < $taille_x } {incr i} {
frame .d.modele.l$i
frame .d.aire.l$i
for {set j 0} { $j < $taille_y } {incr j} {
set num [expr $i * $taille_y + $j ]
set liste [concat $liste $num]
global modele$num aire$num
set modele$num " "
set aire$num " "
# puts [concat $num $i $j ]
button .d.modele.l$i.c$j -textvar modele$num \
-bd 1 -disabledfore black
button .d.aire.l$i.c$j -textvar aire$num -bd 1\
-disabledfore black \
-command "click $num"
pack .d.modele.l$i.c$j .d.aire.l$i.c$j -side left -fill both \
-expand true
}
pack .d.modele.l$i .d.aire.l$i -side top -fill both -expand true
}
set liste [randomize $liste]
foreach i $liste {
do_click modele $i 1
}
pack .d.modele .d.sep .d.aire -side left -fill both -expand true \
-padx 0
pack .d -fill both -expand true -anchor center
}
# --------------------------------------------------------------------
# main
#
set widg_cnt 0
# menu
frame .infos
frame .mbar -bd 2 -relief raised
menubutton .mbar.menu -text tkBrain -underline 0 -menu .mbar.menu.menu
menubutton .mbar.help -text Help -underline 0 -menu .mbar.help.help
menu .mbar.menu.menu
.mbar.menu.menu add command -label "Undo" -underline 0 -command undo
.mbar.menu.menu add command -label "Retry" -underline 0 -command retry
.mbar.menu.menu add command -label "New Game" -underline 0 -command new_game
.mbar.menu.menu add command -label "Resize..." -underline 2 -command resize
.mbar.menu.menu add separator
.mbar.menu.menu add command -label "Quit" -underline 0 -command "destroy ."
menu .mbar.help.help -tearoff 0
.mbar.help.help add command -label "About..." -command about
.mbar.help.help add command -label "Help..." -command help
pack .mbar.menu -side left
pack .mbar.help -side right
tk_menuBar .mbar .mbar.menu .mbar.help
focus .mbar
# text area
label .infos.text -relief sunken -border 1
pack .mbar -fill x -in .infos
pack .infos.text -fill x
pack .infos -fill x -anchor n
# to be destroy by initialise
frame .d
initialise
# eof ---------------------------------------------------------------
# Local Variables:
# mode: tcl
# End: